package com.sczmgk.hotline_pzh.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.sczmgk.hotline_pzh.R;
import com.sczmgk.hotline_pzh.entity.ModuleInfo;
import com.sczmgk.hotline_pzh.mainhomeUiView.MainHomeApplication;

import java.util.List;

import static com.sczmgk.hotline_pzh.MyApp.bitmapUtils;

public class MainHomeAdapter extends BaseAdapter {

    private final Context context;
    public List<ModuleInfo> mList;
    private int mLayoutId;

    /**
     * 数据和布局
     */
    public MainHomeAdapter(List<ModuleInfo> list, int layoutId, Context context) {
        super();
        this.context = context;
        mList = list;
        mLayoutId = layoutId;
        bitmapUtils.configDefaultLoadFailedImage(R.drawable.default_center) ;
    }

    public void setList(List<ModuleInfo> list) {
        this.mList = list;
    }

    @Override
    public int getCount() {
        return this.mList == null ? 0 : this.mList.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        ViewHolder holder = null;

        if (convertView == null) {

            convertView = View.inflate(parent.getContext(), mLayoutId, null);
            holder = new ViewHolder();

            //功能GridView
            holder.ivImageIcon = (ImageView) convertView.findViewById(R.id.iv_function_item);
            holder.tvTitle = (TextView) convertView.findViewById(R.id.tv_function_item_title);
            holder.newMsgPoint = (TextView) convertView.findViewById(R.id.iv_new_msg_point);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        //功能GridView
        ModuleInfo functionBean = mList.get(position);

        holder.tvTitle.setText(functionBean.title);

        if (functionBean.picurl != null && functionBean.picurl.length() > 0) {
            bitmapUtils.display(holder.ivImageIcon, functionBean.picurl);
        }else{
            holder.ivImageIcon.setImageResource(R.drawable.default_center);
        }

        if (mList.get(position).code.equals(MainHomeApplication.MODULE_CHAT)) {

            holder.newMsgPoint.setTag(MainHomeApplication.TAG_NEW_MSG_POINT);
        }

        return convertView;
    }

    private class ViewHolder {

        //功能
        private ImageView ivImageIcon;
        private TextView tvTitle;
        private TextView newMsgPoint;

    }
}
